Skip to content

fix(cache-ledger): attribute tail-append misses to provider-side, not seam suspects (#2059) - #2061

Merged
ranxianglei merged 2 commits into
masterfrom
2026-10-04_seam-tail-append-providerside
Oct 4, 2026
Merged

ranxianglei merged 2 commits into
masterfrom
2026-10-04_seam-tail-append-providerside

Conversation

@ranxianglei

Copy link
Copy Markdown
Owner

Summary

Fixes #2059.

detectSeam (src/cache-ledger.ts) separates "upstream-side" cache misses from "local mid-history prefix breaks" before booking an unexplained residual as a seam-suspect. Its upstream-side test was lcpBytes >= cur.length — i.e. the entire outbound body must match the previous request byte-for-byte. But a conversation appends new messages at the tail every turn, so lcpBytes can never reach cur.length; the condition almost never held. Result: the most ordinary "tail append + upstream did not serve its cache" event was booked as seamSuspects and surfaced as ⚠ CACHE SEAM (suspected mid-history prefix breaks).

The stronger, already-computed signal went unused: seamLcp returns msgIndex/prevMsgs. msgIndex === prevMsgs means the previous request's entire message list survives byte-identically as this request's prefix and only the tail grew — no prefix break exists. That is provider-side (TTL expiry / eviction / relay node rotation), not a rebuild seam.

Change

In detectSeam, after the exact-resend check, add: when msgIndex === prevMsgs && curMsgs > prevMsgs (a pure tail growth) and both bodies are within SEAM_BODY_CAP, book the miss into providerSideMisses / providerSideMissed instead of seamSuspects.

The cap guard is load-bearing: a body truncated at SEAM_BODY_CAP fails JSON.parse inside seamLcp.msgsOf → zero messages → would fake the msgIndex === prevMsgs === 0 signature; truncated pairs therefore keep the old (conservative) seam-suspect path. prevMsgs > 0 additionally keeps an empty prior list out.

Untouched, and pinned by tests:

  • 0 < msgIndex < prevMsgs (real mid-history break) → still seam-suspect
  • msgIndex == 0 (head break) → still seam-suspect
  • exact-resend (lcpBytes >= cur.length) → still provider-side
  • rewind (curMsgs < prevMsgs) → still HISTORY REWOUND

No forwarded bytes change — pure observation layer (§7.3 root-cause-first: fix the attribution at the decision point, don't filter payload in transit).

⚠ Behavior change (attribution bucketing + report wording)

This intentionally changes how PAST tail-append misses are bucketed and worded:

Before After
Tail-append turn + unexplained miss counted in agg.seamSuspects, shown under ⚠ CACHE SEAM (suspected mid-history prefix breaks) counted in agg.providerSideMisses, shown under ▲ PROVIDER-SIDE MISS
PROVIDER-SIDE MISS header / text "(wire was byte-stable)" / "the outbound body matched the previous request's prefix" "(previous request's message list fully preserved)" / "the previous request's entire message list is byte-for-byte a prefix of this request (any difference is only the appended tail)"

Why: the old bucketing mis-attributed the dominant class of large misses as rebuild seams when they were upstream TTL/eviction/rotation. Per the issue's local ledger (289 sessions), 78/87 large-miss events (89.7%) satisfied msgIndex == prevMsgs — ~5.7M input tokens — all previously shown as CACHE SEAM. This moves them to the correct bucket. Genuine mid-history / head breaks are unaffected (pinned by tests). No closure math changes — only which diagnostic bucket the residual lands in.

Existing test seam events are bounded (ring keeps the last 8) updated: its first transition (["a"] -> ["a","v0"]) is a pure tail append, now provider-side, so seamSuspects goes 12 → 11 and providerSideMisses gains 1. The ring-cap assertion is unchanged.

Config surface

None. No new config key, env var, CLI flag, or report field. Reuses the existing providerSideMisses / providerSideMissed counters and the existing PROVIDER-SIDE MISS report section. SEAM_BODY_CAP / SEAM_EVENTS_CAP constants unchanged.

Tests

New in tests/cache-seam-detector.test.ts:

  • tail-append (single + multi-message) → provider-side, not seam
  • real mid-history break (0 < msgIndex < prevMsgs) stays seam-suspect
  • head break (msgIndex == 0) stays seam-suspect
  • over-cap prior body keeps an append miss a seam-suspect (cap guard), never a false provider-side

Verification on rebased master (v0.1.182): typecheck clean, build green, targeted suites cache-seam-detector + issue1891-seam-nobaseline + cache-ledger = 57/57 pass. Full regression left to CI.

… seam suspects (#2059)

detectSeam's upstream-side test demanded the whole body match byte-for-byte (lcpBytes >= cur.length), which a growing conversation never satisfies because each turn appends new messages at the tail. So the common 'tail append + upstream did not serve its cache' case fell through to a false seam-suspect (CACHE SEAM), while the stronger signal seamLcp already computes -- msgIndex == prevMsgs, i.e. the previous request's entire message list survives byte-identically as this request's prefix -- went unused.

When msgIndex === prevMsgs and curMsgs > prevMsgs (a pure tail growth) and both bodies are within SEAM_BODY_CAP (a truncated body parses to zero messages and would fake the msgIndex == prevMsgs == 0 signature), book the miss as providerSideMisses instead of seamSuspects. 0 < msgIndex < prevMsgs (real mid-history break) and msgIndex == 0 (head break) keep their original seam-suspect classification, pinned by tests. No forwarded bytes change -- pure observation layer.

PROVIDER-SIDE MISS report wording updated to cover both the exact-resend and tail-append cases.
@github-actions

github-actions Bot commented Oct 4, 2026 •

Copy link
Copy Markdown
Contributor

📦 Built Package Artifact

Branch: 2026-10-04_seam-tail-append-providerside (d11fa20)

Option A — Install this exact build (recommended)

npm install -g billion-context@0.0.2106-pr.2061.e8d23b4

This exact version is immutable and reproducible. The rolling pr tag tracks the newest test build across all PRs.

Option B — Download artifact

  1. Download the artifact from the Actions run
  2. Extract the tarball and install:
tar xzf billion-context-pr2061.tgz
npm install -g package

Install instructions are refreshed on each push.

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

收到,开始 review:拉分支核对代码 → 对照原 issue #2059 的方向与判据 → 检查 CI。另注意到存在并行 PR #2060(同样 Fixes #2059),会一并比对后给出结论。

…d criterion (#2059)

Labeled in-passing fix from review of this PR: after the tail-append
reclassification, providerSideMisses also covers samples whose bodies were
NOT fully byte-stable (message list preserved, tail appended), so the old
'wire was byte-stable' wording in the web-panel tip (zh+en) and the
providerSideMisses doc comment no longer matched the code fact. Updated to
state the actual criterion.
@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 Review complete — verdict up front: code is correct, tests are real, CI fully green; but this PR duplicates #2060 (opened by @ADkun 2 minutes earlier, same issue) and is a strict subset of its scope. Owner needs to pick one; I have closed neither. Details below.

结构化七问

  1. 是什么问题:seam 检测器的 provider-side 判定要求整个出站体字节全同(lcpBytes >= cur.length),而对话每轮都在追加消息、永远满足不了——于是最常见的「追加一轮 + 上游没给缓存」被误报成 ⚠ CACHE SEAM (suspected mid-history prefix breaks)(issue [obs] detectSeam: tail-append turns are misattributed as local prefix breaks (msgIndex == prevMsgs is already computed but unused) #2059 实测 289 会话中 78/87 大 miss 事件、约 5.7M input tokens 被错标),引导运维去追不存在的重建缝。
  2. 什么原因导致的:判别器用错了粒度。seamLcp 其实已经算出了正确的判据——消息级 LCP:msgIndex === prevMsgs 意味着上一请求的整条消息列表逐字节存活为当前请求的前缀,即不存在中段历史断裂;但旧代码只用了体级字节 LCP,没消费这个更强的信号。
  3. 怎么修复的:在 detectSeam 的 exact-resend 检查之后加一个分支:两体均未超 SEAM_BODY_CAP(512 KiB) + prevMsgs > 0 + msgIndex === prevMsgs + curMsgs > prevMsgs → 记入 providerSideMisses。cap 守卫是承重的:截断到 cap 的体 JSON.parse 失败 → 解析出 0 条消息 → 会伪造出 msgIndex == prevMsgs == 0 签名(我逐条验证过单侧/双侧截断的所有组合都不会误触发新分支);报告措辞同步改为与新判据一致。纯观测层,转发路径零改动。
  4. 是否有回归问题:逐项过完既有行为——rewind(curMsgs < prevMsgs,先判)不变;exact-resend(先判,无重复计数)不变;真实中段断裂 0 < msgIndex < prevMsgs 仍留 seam-suspect(有测试钉住);头部断裂 msgIndex == 0 仍留(有测试钉住);截断对保持保守归 seam-suspect(有测试钉住)。披露的行为变更:tail-append 类未解释 miss 从 seam 桶移入 provider-side 桶(这正是修复目的);PROVIDER-SIDE MISS 小节标题/正文措辞更新。review 中发现并顺手修掉一处:PR 自己的语义扩大使 web 面板 i18n tip(zh+en det.attr_provider_tip「字节稳定」表述)和 providerSideMisses 接口文档注释与代码事实不符——已推标注提交 d11fa207(docs-only,typecheck + web-i18n + seam 套件本地复验通过)。
  5. 是否破坏缓存:否。只改 /acp-cache 诊断报告的计数分桶与文案;前缀缓存、重试、增量游标、去重键均不受影响。
  6. 是否改了设计方向:否。同一账本架构、同一分支顺序、同一保守桶哲学,未引入新机制。
  7. 是否引入新配置:无 env/config/CLI 变化。

验证证据

  • CI(head 8b4f65d5):15/15 全绿(含 ubuntu/windows × node22/24 全套件、registry e2e、各 real-client lane)。
  • 本地在 8b4f65d5:npm run typecheck 干净;定向套件 cache-seam-detector + issue1891-seam-nobaseline + cache-ledger = 57/57 pass。
  • head 位于当前 master(3a7308ab)之上,非陈旧基线。
  • d11fa207(本 review 推送的措辞同步)CI 重跑中。

⚠️ 重复 PR:#2060(@ADkun,issue 作者本人,早 2 分钟开)

两个 PR 修同一个核心缺陷,核心逻辑等价(#2060 用 seamTailAppendOnly() helper 表达同一条件)。差异点:

维度 #2061(本 PR) #2060(@ADkun)
核心修复 ✅ ✅(等价)
对既有 exact-resend 臂也加 under-cap 守卫 ❌ 不动 ✅ ——额外行为变更:超 cap 的字节全同对从 providerSideMisses 回退到 seamSuspects(「不在截断证据上断言」,保守方向;属产品判断,需 owner 明示签核)
web 面板 i18n 措辞同步 ✅(本 review 补的 d11fa207) ✅
同消息数情形(消息列表相同、差异在 messages 之外如 tools/system 漂移) 仍留 seam-suspect(更保守) 也归 provider-side(其 helper 不要求 curMsgs > prevMsgs)
基线 当前 master 落后 master,合并前需 rebase
CI 15/15 绿 15/15 绿

按机制优先原则(owner policy 2026-10-01),「SEAM_BODY_CAP 截断扭曲归因取证」是一个机制族,#2060 覆盖得更完整;但按外部贡献者保护政策,我不会关闭或替代 @ADkun 的 PR。建议二选一:① 要最小行为差、最快落地 → 合本 PR(已含措辞同步);② 要机制族完整处理(含 over-cap exact-resend 守卫)→ rebase 后合 #2060 并对那一处额外行为变更显式签核,本 PR 随之关闭。

另记录一个既有残留(非本 PR 引入、不扩本 PR 范围,按机制族留作 checklist 项):cur 体超 cap 而 prev 正常时,截断侧解析为 0 条消息会命中既有的 rewind 分支(curMsgs(0) < prevMsgs),把「相邻轮次体积暴涨」误标为 HISTORY REWOUND——master 上已存在,需要的话另行立项。

收工判定:本 PR 自身可合(auto-merge gate 各项满足,纯 fix、回归测试齐、绿在最新 master 之上);最终合并哪个由 owner 在 #2060/#2061 之间拍板。

@ranxianglei

Copy link
Copy Markdown
Owner Author

🤖 Powered by ework · qwen3.8-27b

[bot] 🏷 补充确认:措辞同步提交 d11fa207 的 CI 已全绿(15/15,含全套件与 e2e lanes)。本 PR 现处于可合状态;#2060/#2061 二选一仍待 owner 拍板。

@ranxianglei
ranxianglei merged commit 2be41f9 into master Oct 4, 2026
15 checks passed
@github-actions

github-actions Bot commented Oct 4, 2026

Copy link
Copy Markdown
Contributor

📦 Merged Build Published

This merge to master was published as 0.0.11-master.2be41f9 (rolling master tag).

npm install -g billion-context@0.0.11-master.2be41f9  # this exact build
npm install -g billion-context@master  # always the newest merged build

npm view billion-context@master prints the current version at any time.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[obs] detectSeam: tail-append turns are misattributed as local prefix breaks (msgIndex == prevMsgs is already computed but unused)

1 participant